<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['op']=='main'){
	$return['setting']=$this->plugin_setting('basic');
	$return['list']=array();
	set_html('title',$$basic['title']?$basic['title']:'门店地图');
	$return['params']=array();
	exi($return);
}
// js 获取数据
if($_RQ['op']=='getlist'){
	if($_RQ['lat'] || $_RQ['lng']){
		$lat_lng=array('lat'=>$_RQ['lat'],'lng'=>$_RQ['lng']);
		$this->session('lat_lng',$lat_lng);
	}
	$lat_lng=$this->session('lat_lng')?:array('lat'=>'31.235929','lng'=>'121.480539');
	$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1);
	$pindex = max(1, 1);
	$psize = 500;
	$total='';
	$fields=array('id','logo','title','address','mobile','onlinetime','lat_lng','pcd','out_juli','out_pay');
	$list=pdo_getslice('jy_weishop_store',$params,array($pindex,$psize),$total,$fields,'','id desc');
	foreach($list as $k=>&$v){
		$v['pcd']=json_decode($v['pcd'],true);
		if(trim($_RQ['province']) && $v['pcd']['province']!=trim($_RQ['province'])){
			unset($list[$k]);
			continue;
		}
		if(trim($_RQ['city']) && $v['pcd']['city']!=trim($_RQ['city'])){
			unset($list[$k]);
			continue;
		}
		if(trim($_RQ['district']) && $v['pcd']['district']!=trim($_RQ['district'])){
			unset($list[$k]);
			continue;
		}
		$v['lat_lng']=json_decode($v['lat_lng'],true);
		$v['juli']=cfc('tools')->getdistance($lat_lng['lng'],$lat_lng['lat'],$v['lat_lng']['lng'],$v['lat_lng']['lat']);
		$v['juli']=intval($v['juli']/1000);
		$v['onlinetime']=json_decode($v['onlinetime'],true);
		if($v['juli']<10){
			$v['peisongtime']=$v['juli']*8+38;
		}else if($v['juli']<30){
			$v['peisongtime']=$v['juli']*3+38;
		}else{
			$v['peisongtime']=$v['juli']*1+38;
		}
		$v['out_juli']=$v['out_juli']>0?$v['out_juli']:0;
	}
	paixu($list,'juli');
	if($_RQ['limit']){
		$list=array_slice($list,0,intval($_RQ['limit']));
	}else{
		$page=intval($_RQ['page'])?:1;
		$start=($page-1)*10;
		$list=array_slice($list,$start,10);
	}
	$return['list']=$list;
	if($_RQ['islocation']){
		$_SESSION['store_id']=$list[0]['id'];
	}
	exi($return);
}
if($_RQ['op']=='get_juli'){
	if($_RQ['lat'] || $_RQ['lng']){
		$lat_lng=array('lat'=>$_RQ['lat'],'lng'=>$_RQ['lng']);
	}
	$store_id=intval($_RQ['store_id'])?:$this->session('store_id');
	$storelat_lng=pdo_getcolumn('jy_weishop_store',array('id'=>$store_id),'lat_lng');
	$storelat_lng=json_decode($storelat_lng,true);
	$juli=cfc('tools')->getdistance($lat_lng['lng'],$lat_lng['lat'],$storelat_lng['lng'],$storelat_lng['lat']);
	$juli=intval($juli/1000);
	$juli=$juli?:0;
	exi($juli);
}
if($_RQ['op']=='out_time'){
	$store_id=intval($_RQ['store_id'])?:$this->session('store_id');
	$merch_id=intval($_RQ['merch_id']);
	$post['set_timebox']=$return['pluginset']['set_timebox'];
	$post['peisong_timebox']=$return['pluginset']['peisong_timebox'];
	$post['out_pay']=$_RQ['out_pay'];
	if($merch_id){
		$out_time = model('jy_merch/model')->out_times($merch_id,$post);
	}else{
		$out_time = model('jy_weishop/store')->out_times($store_id,$post);
	}
	$timestamp = TIMESTAMP + 60 * 30;
	$data = model('jy_weishop/store')->array_order($timestamp, $out_time['timestamp']);
	$predict_index = array_search($data, $out_time['timestamp']);
	$predict_day = $out_time['days'][0];
	$predict_time = $out_time['times'][$predict_index]['start'].'~'.$out_time['times'][$predict_index]['end'];
	$text_time = "尽快送达";
	$return['timestamp']=$timestamp;
	$return['predict_time']=$predict_time;
	$return['out_time']=$out_time;
	exi($return);
}
?>